package com.atguigu.srb.core;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

public class CodeGenerator {
    @Test
    public void genCode(){
        //1.创建代码生成器
        AutoGenerator generator = new AutoGenerator();
        //2.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//获取当前项目地址
        gc.setOutputDir(projectPath+"/src/main/java");//设置输出地址
        gc.setOpen(true);
        gc.setServiceName("%sService");
        gc.setIdType(IdType.AUTO);
        gc.setSwagger2(true);
        generator.setGlobalConfig(gc);
        //3,数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/srb_core?characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        generator.setDataSource(dsc);
        //4.配置包
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.atguigu.srb.core");
        pc.setEntity("pojo.entity");
        generator.setPackageInfo(pc);
        //5.策略配置
        StrategyConfig stc  = new StrategyConfig();
        stc.setNaming(NamingStrategy.underline_to_camel);//数据库映射到实体类的命名策略
        stc.setColumnNaming(NamingStrategy.underline_to_camel);//数据库字段名映射到实体类属性的命名策略
        stc.setEntityLombokModel(true);
        stc.setLogicDeleteFieldName("is_deleted");//逻辑删除字段名
        stc.setEntityBooleanColumnRemoveIsPrefix(true);//去掉布尔值的is前缀（确保tinyint（1））
        stc.setRestControllerStyle(true);//使用restcontroller风格的注解
        generator.setStrategy(stc);
        generator.execute();





    }
}
